import api from '@/api'
import libs from '@/libs'

export default {
  namespaced: true,
  state: {
    // 生产线
    line: libs.store.getItem('pre_line') || [],
    // 桥梁
    bridge: libs.store.getItem('pre_bridge') || [],
    // 工序
    procedure: libs.store.getItem('pre_procedure') || [],
    // 预制梁
    prebeams: libs.store.getItem('pre_prebeams') || [],
    // 钢筋设备
    steelDevice: libs.store.getItem('pre_steel_device') || [],
    // 钢筋设备类型
    steelDeviceType: libs.store.getItem('pre_steel_device_type') || [],
  },
  /**
   * actions 通过 this.$store.dispatch 来调用
   * 可以异步请求，放置一些业务代码
   */
  actions: {
    // 生产线
    async getLine({ state }) {
      let lineRes = await api.GET_PRODUCT_LINE()
      libs.store.setItem('pre_line', lineRes.list)
      state.line = lineRes.list
    },
    // 桥梁
    async getBridge({ state }) {
      let bridgeRes = await api.GET_ALL_BRIDGE_LIST()
      libs.store.setItem('pre_bridge', bridgeRes.list)
      state.bridge = bridgeRes.list
    },
    // 工序
    async getProcedure({ state }) {
      let procedureRes = await api.GET_ALL_PROCEDURE_LIST()
      procedureRes.list =  procedureRes.list.sort((a, b) => a.procedureOrder - b.procedureOrder)
      libs.store.setItem('pre_procedure', procedureRes.list)
      state.procedure = procedureRes.list
    },
    // 预制梁
    async getPrebeams({ state }) {
      let prebeamsRes = await api.GET_PREBRIDGE_LIST({pageNo: 1, pageSize: 100000})
      libs.store.setItem('pre_prebeams', prebeamsRes.list)
      state.prebeams = prebeamsRes.list
    },
    // 钢筋设备
    async getSteelDevice({ state }) {
      let steelDeviceRes = await api.GET_STEEL_DEVICE_LIST({ pageNo: 1, pageSize: 10000 })
      libs.store.setItem('pre_steel_device', steelDeviceRes.list)
      state.steelDevice = steelDeviceRes.list
    },
    // 钢筋设备类型
    async getSteelDeviceType({ state }) {
      let steelDeviceTypeRes = await api.GET_STEEL_TYPE_LIST({ pageNo: 1, pageSize: 10000 })
      libs.store.setItem('pre_steel_device_type', steelDeviceTypeRes.list)
      state.steelDeviceType = steelDeviceTypeRes.list
    },
    /**
     * @description 设置数据
     * @param {Object} context
     * @param {*} info info
     */
    async init({ dispatch }) {
      await Promise.all([
        dispatch('getLine'),
        dispatch('getBridge'),
        dispatch('getProcedure'),
        dispatch('getPrebeams'),
        dispatch('getSteelDevice'),
        dispatch('getSteelDeviceType'),
      ])
      
      // // 每分钟获取一次数据
      // setTimeout(() => {
      //   dispatch('init')
      // }, 1000 * 60)
    },
  },
  /**
   * mutations 通过 this.$store.commit() 来调用
   * 专注于修改 state 不能异步 state相关的操作尽量在 mutations 中完成
   */
  mutations: {
    /**
     * @description 设置数据
     * @param {Object} state
     * @param {*} info info
     */
    set(state, info) {
      state.info = info;
    },
  },
};
